#include "ANTLRv4LexerBase.hpp"
#include "cpp-gen/ANTLRv4Lexer.h"

using namespace antlr4;
ANTLRv4LexerBase::ANTLRv4LexerBase(antlr4::CharStream *input) : Lexer(input)
{
}

ANTLRv4LexerBase::~ANTLRv4LexerBase()
{
}

Token *ANTLRv4LexerBase::emit()
{
    if (type == ANTLRv4Lexer::ID)
    {
        auto firstChar = this->_input->getText(antlr4::misc::Interval(this->tokenStartCharIndex, this->tokenStartCharIndex));
        if (isupper(firstChar.at(0)))
        {
            type = ANTLRv4Lexer::TOKEN_REF;
        }
        else
        {
            type = ANTLRv4Lexer::RULE_REF;
        }

        if (_ruleType == Token::INVALID_TYPE)
        {
            _ruleType = type;
        }
    }
    else if (type == ANTLRv4Lexer::SEMI)
    {
        _ruleType = Token::INVALID_TYPE;
    }
    return Lexer::emit();
}

void ANTLRv4LexerBase::handleBeginArgAction()
{
    if (inLexerRule())
    {
        pushMode(ANTLRv4Lexer::LexerCharSet);
        more();
    }
    else
    {
        pushMode(ANTLRv4Lexer::ArgAction);
    }
}

bool ANTLRv4LexerBase::inLexerRule()
{
    return _ruleType == ANTLRv4Lexer::TOKEN_REF;
}
